<template>
  <el-container class="home-container">
    <!-- 页面头部 -->
    <el-header>
      <div>
        <img src="@/assets/image/management.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体内容 -->
    <el-container>
      <!-- 页面侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 可折叠菜单栏 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#333744"
                          text-color="#fff"
                          active-text-color="#409eff"
                          unique-opened
                          :collapse='isCollapse'
                          :collapse-transition="false"
                          :router="true"
                          :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="(item, index) in this.menuList" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[index]" style="padding-right:5px;"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单-->
            <el-menu-item :index="'/' + i.path" v-for="i in item.children" :key="i.id" @click="saveNavState('/' + i.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <span>{{i.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconList: [
        'iconfont icon-users',
        'iconfont icon-3702mima',
        'iconfont icon-shangpin',
        'iconfont icon-danju',
        'iconfont icon-tijikongjian'
      ],
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.menusList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 折叠菜单栏
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    // 获取左侧列表数据
    async menusList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error()
      this.menuList = res.data
    }
  }
}
</script>

<style lang='less' scoped>
.home-container {
  height: 100vh;
}

.el-header {
  background-color: #373d4e;
  color: #333;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    img {
      padding-right: 20px;
    }
  }
}

.el-aside {
  background-color: #333744;
  color: #333;
}

.el-menu {
  border-right: 0;
}

.toggle-button {
  background: #4a5064;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  cursor: pointer;
  letter-spacing: .2em;
  color: #fff;
}

.el-main {
  background-color: #eaedf1;
  color: #333;
}
</style>
